package com;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

public class WanhoPipeline implements Pipeline {


    public void process(ResultItems resultItems, Task task) {

        System.out.println("process");
        //获取住区过程中保存的数据
        Vector<PageVo> pageVoVector=resultItems.get("e_list");
        //持久到文件中
        PrintWriter printWriter= null;
        try {
            try {
                printWriter = new PrintWriter(new FileWriter("wanho.txt",true));
            } catch (IOException e) {
                e.printStackTrace();
            }
            for(PageVo vo:pageVoVector)
            {
                printWriter.println(vo);
                printWriter.flush();
                saveImg(vo.getImg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }
    }
    private void saveImg(String img) {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        InputStream is=null;
        String Imgurl="http://www.wanho.net/"+img;
        try {
            URL url=new URL(Imgurl);
            URLConnection connection=url.openConnection();
            is=connection.getInputStream();
            bis=new BufferedInputStream(is);
            File photofile=new File("photo");
            if(!photofile.exists())
            {
                photofile.mkdirs();
            }
            String imgName=img.substring(img.lastIndexOf("/")+1);
            File saveFile=new File(photofile,imgName);

             bos=new BufferedOutputStream(new FileOutputStream(saveFile));
            byte[] bs=new byte[1024];
            int len;
            while((len=bis.read(bs))!=-1)
            {
                bos.write(bs,0,len);
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
